package com.quanyan.authority.service.impl;

import com.quanyan.authority.cache.AuthorityRedisTemplate;
import com.quanyan.authority.constants.AuthorityConstants;
import com.quanyan.authority.mapper.RoleMenuMapper;
import com.quanyan.authority.mapper.TbRoleMenuMapper;
import com.quanyan.authority.model.db.TbRoleMenu;
import com.quanyan.authority.model.db.TbRoleMenuExample;
import com.quanyan.authority.service.RoleMenuService;
import com.quanyan.authority.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2016/8/1.
 */
@Service
public class RoleMenuServiceImpl implements RoleMenuService {

    @Autowired
    private TbRoleMenuMapper tbRoleMenuMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private AuthorityRedisTemplate authorityRedisTemplate;


    public List<TbRoleMenu> getMenuId(Integer roleId){
        TbRoleMenuExample example = new TbRoleMenuExample();
        TbRoleMenuExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        return  tbRoleMenuMapper.selectByExample(example);
    }


    @Override
    public void batchAddRoleMenu(List<TbRoleMenu> list) {
        roleMenuMapper.batchAdd(list);
       // List<TbRoleMenu> lst = (List<TbRoleMenu>)authorityRedisTemplate.get(AuthorityConstants.ROLE_MENU_REDIS_KEY);
       // lst.addAll(list);
       // authorityRedisTemplate.set(AuthorityConstants.ROLE_MENU_REDIS_KEY,list);
    }

    @Override
    public void batchDelRoleMenu(List<TbRoleMenu> ids) {
        roleMenuMapper.batchDelete(ids);
       // List<TbRoleMenu> lst = (List<TbRoleMenu>)authorityRedisTemplate.get(AuthorityConstants.ROLE_MENU_REDIS_KEY);
      //  lst.removeAll(ids);
    }

    @Override
    public void initRoleMenu() {
        List<TbRoleMenu> list = tbRoleMenuMapper.selectByExample(new TbRoleMenuExample());
        for(TbRoleMenu rm :list){
            rm.setId(null);
        }
      authorityRedisTemplate.set(AuthorityConstants.ROLE_MENU_REDIS_KEY,list);

    }
}
